package com.example.mongodb.service.impl;

import com.example.mongodb.entity.DishesInfo;
import com.example.mongodb.mongodb.DataQueueService;
import com.example.mongodb.repository.DishesInfoRepository;
import com.example.mongodb.service.DishesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : zhayh
 * @date : 2021-5-16 19:39
 * @description :
 */

@Service
public class DishesServiceImpl implements DishesService {
    @Resource
    private DishesInfoRepository dishesInfoRepository;
    @Resource
    private DataQueueService dataQueueService;

    @Override
    public DishesInfo addDishes(DishesInfo dishesInfo) {
        DishesInfo result = dishesInfoRepository.save(dishesInfo);
        if (result.getRestaurantInfo() != null) {
            dataQueueService.sendUpdateMessage(dishesInfo.getRestaurantInfo().getId());
        }
        return result;
    }

    @Override
    public DishesInfo updateDishes(DishesInfo dishesInfo) {
        DishesInfo result = dishesInfoRepository.save(dishesInfo);
        if (result.getRestaurantInfo() != null) {
            dataQueueService.sendUpdateMessage(dishesInfo.getRestaurantInfo().getId());
        }
        return result;
    }

    @Override
    public Boolean deleteDishes(Long id) {
        DishesInfo dishesInfo = dishesInfoRepository.findById(id).orElse(null);
        if (dishesInfo != null) {
            dishesInfoRepository.delete(dishesInfo);
            if (dishesInfo.getRestaurantInfo() != null) {
                dataQueueService.sendUpdateMessage(dishesInfo.getRestaurantInfo().getId());
            }
            return true;
        }
        return false;
    }
}
